import { Component, OnInit } from '@angular/core';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@core/services/http.client';

@Component({
  selector: 'app-class',
  templateUrl: './class.component.html'
})
export class ClassComponent implements OnInit {
    selectValue1;
    selectValue2;
    popTitle;
    loading = false;
    modalVisible2 = false;
    Cname = '';
    Cschool = '';
    CTeacher = '';
    CNum = '';
    _allChecked = false;
    _indeterminate = false;
    _displayData = [];
    _checkTable = [];
    _checkLength = 0;
    _q = {pi: 1, ps: 10};
    option1 = [
        { value: '', label: '请选择' },
        { value: '0', label: '老师1' },
        { value: '1', label: '老师2' },
        { value: '2', label: '老师3' }
    ];
    option2 = [
        { value: '', label: '请选择' },
        { value: '0', label: '在读' },
        { value: '1', label: '结业' },
        { value: '2', label: '作废' }
    ];
    saveState = '';
    searchValue = '';
    serchName = {};
    ids = [];
    _loading = false;
    formNews = {};
    data = [];
    Status = [];
    sta = '';

    ngOnInit() {
        this.getData();
    }

    getData() {
        // this._loading = true;
        this.http.post('EduManagement/class/list', this._q).subscribe(res => {
            console.log(res);
            // this._loading = false;
            // this.data = res;
        });
        this.data = [
            {
                C_id: '1',
                C_name: 'XX一班',
                C_college: 'aaa',
                C_count: '32',
                C_teacher_name: '老师1',
                C_teacher_id: '0',
                C_status: '2'
            },
            {
                C_id: '2',
                C_name: 'XXee二班',
                C_college: 'aaa',
                C_count: '32',
                C_teacher_name: '老师3',
                C_teacher_id: '2',
                C_status: '1'
            },
            {
                C_id: '3',
                C_name: 'XX三班',
                C_college: 'aaa',
                C_count: '32',
                C_teacher_name: '老师2',
                C_teacher_id: '1',
                C_status: '0'
            }
        ];
        this.data.map(d => {
            if (d.C_status == '0') {
                this.sta = '在读';
            } else if (d.C_status == '1') {
                this.sta = '结业';
            }else if (d.C_status == '2') {
                this.sta = '作废';
            }
            this.Status.push(this.sta);
        });
    }
    save() {
        this.loading = true;
        setTimeout(() => {
            this.modalVisible2 = false;
            this.loading = false;
            this.createMessage('success', '操作成功！');
        }, 500);
        if (this.saveState === 'add') {
            this.formNews = {C_name: this.Cname, C_college: this.Cschool, C_teacher_id: this.selectValue1, C_status: this.selectValue2 }
        }else {
            this.formNews = {C_id: '', C_name: this.Cname, C_college: this.Cschool, C_teacher_id: this.selectValue1, C_status: this.selectValue2  }
        }
        this.http.post('EduManagement/class/' + this.saveState, this.formNews).subscribe(res => {
            this.loading = false;
            this.getData();
            console.log(res);
            // this.createMessage('success', '操作成功！');
        });
    }


    modal (state, data) {
        if (state === 'add') {
            this.modalVisible2 = true;
            this.Cname = '';
            this.Cschool = '';
            this.CNum = '';
            this.selectValue1 = '';
            this.selectValue2 = '';
            this.popTitle = '新建班级';
            this.saveState = 'add';
        }else if (state === 'update') {
            this.modalVisible2 = true;
            this.popTitle = '修改班级';
            this.Cname = data.C_name;
            this.Cschool = data.C_college;
            this.CNum = data.C_count;
            this.selectValue1 = data.C_teacher_id;
            this.selectValue2 = data.C_status;
            this.saveState = 'update';
        }

    }

    search () {
        this.serchName = {name: this.searchValue}
        this.http.post('', this.serchName).subscribe(res => {
            console.log(res);
        });
    }

    showConfirm = (data) => {
        this.confirmServ.confirm({
            title  : '您是否确认要删除这项内容',
            onOk() {
                const id = {id: data.id};
                // this.http.post('EduManagement/class/del', id).subscribe(res => {
                //     console.log(res);
                // this.createMessage('success', '删除成功！');
                // });
            },
            onCancel() {
            }
        });
    }

    _displayDataChange($event) {
        this._displayData = $event;
        this._refreshStatus();
    }

    _refreshStatus() {
        const allChecked = this._displayData.every(value => value.checked === true);
        const allUnChecked = this._displayData.every(value => !value.checked);
        const checkTable = this._displayData.filter(value => value.checked);
        this._allChecked = allChecked;
        this._indeterminate = (!allChecked) && (!allUnChecked);
        this._checkTable = checkTable;
        this._checkLength = checkTable.length;
    }

    _checkAll(value) {
        if (value) {
            this._displayData.forEach(data => {
                data.checked = true;
            });
        } else {
            this._displayData.forEach(data => {
                data.checked = false;
            });
        }
        this._refreshStatus();
    }

    allDelete () {
        this._checkTable.map(m => {
            this.ids.push(m.id);
        });
        this.confirmServ.confirm({
            title  : '您是否确认要批量删除选中的' + this._checkLength + '项内容？',
            onOk: () => {
                console.log(this.ids);
                // this.http.post('EduManagement/class/del', {id: this.ids}).subscribe(res => {
                //     console.log(res);
                // this.createMessage('success', '删除成功！');
                // });
            },
            onCancel() {
            }
        });
    }

    info(contentTpl, data, status) {
        this.Cname = data.C_name;
        this.Cschool = data.C_college;
        this.CTeacher = data.C_teacher_name;
        this.CNum = data.C_count;
        this.selectValue1 = data.C_teacher_id;
        this.selectValue2 = status;
        this.confirmServ.info({
            title: '查看详情',
            content: contentTpl
        });
    }

    createMessage = (type, text) => {
        this._message.create(type, `${text}`);
    }

    constructor(
        private confirmServ: NzModalService,
        private http: _HttpClient,
        private _message: NzMessageService
    ) {
    }

}
